package com.woniuxy.yoga.course.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.course.dao.model.OrdinaryCourse;
import com.woniuxy.yoga.course.model.dto.ordinary.OrdinaryCourseByIdDTO;
import com.woniuxy.yoga.course.model.form.ordinary.AmountOrdinaryCourseDurationForm;
import com.woniuxy.yoga.course.model.dto.ordinary.OrdinaryCourseDTO;
import com.woniuxy.yoga.course.model.form.ordinary.AddOrdinaryCourseForm;
import com.woniuxy.yoga.course.model.form.ordinary.QueryAllOrdinaryCourseForm;
import com.woniuxy.yoga.course.model.form.ordinary.QueryCoachOrdinaryCourseForm;
import com.woniuxy.yoga.course.model.form.ordinary.QueryNormalOrdinaryCourseForm;
import com.woniuxy.yoga.course.model.param.ordinary.*;
import com.woniuxy.yoga.course.service.service.OrdinaryCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 普通课程表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/ordinaryCourse")
@Api(tags = "普通课程相关业务接口")
@Validated
public class OrdinaryCourseController {

    @Autowired
    private OrdinaryCourseService ordinaryCourseService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 教练ID查询普通课程信息
     * @param
     * @return
     */
    @PostMapping("/getByCoachId")
    @ApiOperation("教练ID查询普通课程信息接口")
    public Result<List<OrdinaryCourseDTO>> getByCoachId(@RequestBody List<Integer> ids){
        //获取数据
        List<OrdinaryCourseDTO> ordinaryCourseDTOS = ordinaryCourseService.getByCoachId(ids);
        //返回数据
        return Result.getListResult(OrdinaryCourseDTO.class).setMessage("根据教练ID查询普通课程信息成功").setData(ordinaryCourseDTOS);
    }

    /**
     * 新增普通课程
     * @param addOrdinaryCourseForm
     * @return
     */
    @PostMapping("/addOrdinaryCourse.bu")
    @ApiOperation("新增普通课程接口")
    public Result<?> addOrdinaryCourse(@Valid @RequestBody AddOrdinaryCourseForm addOrdinaryCourseForms){
        ordinaryCourseService.addOrdinaryCourse(BeanUtil.toBean(addOrdinaryCourseForms, AddOrdinaryCourseParam.class));
        return Result.getResult().setMessage("新增普通课程成功");
    }

    /**
     * 普通课程开课
     * @param courseId
     * @return
     */
    @GetMapping("/startOrdinaryCourse/{courseId}.co")
    @ApiOperation("普通课程开课接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<?> startOrdinaryCourse(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId){
        ordinaryCourseService.startOrdinaryCourse(courseId);
        return Result.getResult().setMessage("普通课程开课成功");
    }

    /**
     * 普通课程取消
     * @param courseId
     * @return
     */
    @GetMapping("/cancelOrdinaryCourse/{courseId}.bu")
    @ApiOperation("普通课程取消接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<?> cancelOrdinaryCourse(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId){
        ordinaryCourseService.cancelOrdinaryCourse(courseId);
        return Result.getResult().setMessage("普通课程取消成功");
    }

    /**
     * 普通课程完成
     * @param courseId
     * @return
     */
    @GetMapping("/completeOrdinaryCourse/{courseId}.co")
    @ApiOperation("普通课程完成接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<?> completeOrdinaryCourse(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId){
        ordinaryCourseService.completeOrdinaryCourse(courseId);
        return Result.getResult().setMessage("普通课程完成成功");
    }

    /**
     * 普通课程当前人数加一
     * @param courseId
     * @return
     */
    @GetMapping("/increaseCurrentCount/{courseId}")
    @ApiOperation("普通课程当前人数加一接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<?> increaseCurrentCount(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId) throws Exception{
        ordinaryCourseService.increaseCurrentCount(courseId);
        return Result.getResult().setMessage("普通课程当前人数加一成功");
    }

    /**
     * 教室ID查询普通课程
     * @param classroomId
     * @return
     */
    @GetMapping("/getByClassroomId/{classroomId}")
    @ApiOperation("教室ID查询普通课程接口")
    @ApiImplicitParam(name = "classroomId",value = "教室ID")
    public Result<List<OrdinaryCourseDTO>> getByClassroomId(@Valid @NotNull(message = "教室ID不能为空") @PathVariable("classroomId") Integer classroomId){
        //获取数据
        List<OrdinaryCourseDTO> ordinaryCourseDTOS = ordinaryCourseService.getByClassroomId(classroomId);
        //返回数据
        return Result.getListResult(OrdinaryCourseDTO.class).setMessage("根据教室ID查询普通课程成功").setData(ordinaryCourseDTOS);
    }

    /**
     * 查询正常的普通课程
     * @param queryNormalOrdinaryCourseForm
     * @return
     */
    @PostMapping("/queryNormalOrdinaryCourse.us")
    @ApiOperation("用户查询正常的普通课程接口")
    public Result<PageInfo> queryNormalOrdinaryCourse(@RequestBody QueryNormalOrdinaryCourseForm queryNormalOrdinaryCourseForm){
        PageInfo pageInfo = ordinaryCourseService.queryNormalOrdinaryCourse(BeanUtil.toBean(queryNormalOrdinaryCourseForm, QueryNormalOrdinaryCourseParam.class));
        return Result.getResult(PageInfo.class).setMessage("查询正常的普通课程成功").setData(pageInfo);
    }

    /**
     * 商家查询所有的普通课程
     * @param allOrdinaryCourseForm
     * @return
     */
    @PostMapping("/queryAllOrdinaryCourse.bu")
    @ApiOperation("商家查询所有的普通课程接口")
    public Result<PageInfo> queryAllOrdinaryCourse(@Valid @RequestBody QueryAllOrdinaryCourseForm allOrdinaryCourseForm){
        PageInfo pageInfo = ordinaryCourseService.queryAllOrdinaryCourse(BeanUtil.toBean(allOrdinaryCourseForm, QueryAllOrdinaryCourseParam.class));
        return Result.getResult(PageInfo.class).setMessage("商家查询所有的普通课程成功").setData(pageInfo);
    }

    /**
     * 教练查询自己的普通课程
     * @param queryCoachOrdinaryCourseForm
     * @param authenToken
     * @return
     */
    @PostMapping("/queryCoachOrdinaryCourse.co")
    @ApiOperation("教练查询自己的普通课程接口")
    public Result<PageInfo> queryCoachOrdinaryCourse(@RequestBody QueryCoachOrdinaryCourseForm queryCoachOrdinaryCourseForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析token 获取coachId
        Integer coachId = JWTUtil.getCoachId(refreshToken);
        //对象转换
        QueryCoachOrdinaryCourseParam param = BeanUtil.toBean(queryCoachOrdinaryCourseForm, QueryCoachOrdinaryCourseParam.class);
        //教练赋值
        param.setCoachId(coachId);
        //调用方法
        PageInfo pageInfo = ordinaryCourseService.queryCoachOrdinaryCourse(param);
        //返回数据
        return Result.getResult(PageInfo.class).setMessage("教练查询自己的普通课程成功").setData(pageInfo);
    }

    /**
     * 统计该时间段普通课程已完成的总时长
     * @param amountOrdinaryCourseDurationForm
     * @return
     */
    @PostMapping("/amountOrdinaryCourseDuration")
    @ApiOperation("统计普通课程总时长接口")
    public Result<Integer> amountOrdinaryCourseDuration(@RequestBody AmountOrdinaryCourseDurationForm amountOrdinaryCourseDurationForm){
        Integer duration = ordinaryCourseService.amountOrdinaryCourseDuration(BeanUtil.toBean(amountOrdinaryCourseDurationForm, AmountOrdinaryCourseDurationParam.class));
        return Result.getResult(Integer.class).setMessage("统计该时间段普通课程已完成的总时长成功").setData(duration);
    }

    /**
     * 根据普通课程ID查询
     * @param courseId
     * @return
     */
    @GetMapping("/getById/{courseId}")
    @ApiOperation("普通课程ID查询接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<OrdinaryCourseByIdDTO> getById(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId){
        //获取数据
        OrdinaryCourse ordinaryCourse = ordinaryCourseService.getById(courseId);
        //对象转换
        OrdinaryCourseByIdDTO ordinaryCourseByIdDTO = BeanUtil.toBean(ordinaryCourse, OrdinaryCourseByIdDTO.class);
        //返回数据
        return Result.getResult(OrdinaryCourseByIdDTO.class).setMessage("根据普通课程ID查询成功").setData(ordinaryCourseByIdDTO);
    }

    /**
     * 普通课程当前预约人数减一
     * @param courseId
     * @return
     */
    @GetMapping("/reduceCurrentCount/{courseId}")
    @ApiOperation("当前预约人数减一接口")
    @ApiImplicitParam(name = "courseId",value = "普通课程ID")
    public Result<?> reduceCurrentCount(@Valid @NotNull(message = "普通课程ID不能为空") @PathVariable("courseId") Integer courseId){
        ordinaryCourseService.reduceCurrentCount(courseId);
        return Result.getResult().setMessage("普通课程当前预约人数减一成功");
    }
}

